<?php


class WSB_View_Helper_FormSelectTemplate extends Zend_View_Helper_FormSelect
{

	/**
	 * @param string $name
	 * @param mixed $value
	 * @param array|string $attribs
	 * @param string $listsep
	 * @return string
	 */
	public function formSelectTemplate($name, $value = null, $attribs = null, $listsep = "<br />\n")
	{
		$info = $this->_getInfo($name, $value, $attribs, null, $listsep);
        extract($info); // name, id, value, attribs, options, listsep, disable
        $options = $this->_buildOptions();
      	$element = $this->formSelect($name, $value, $attribs, $options, $listsep);
		return $element;
	}

	/**
	 * @return array
	 */
	private function _buildOptions()
	{
        $formValues = Zend_Registry::get('Zend_Form_Values');
        $where = 'deleted=0';
		if(!empty($formValues['Cms_Model_DbTable_Template']['id'])) {
			$where .= ' AND id <> ' . $formValues['Cms_Model_DbTable_Template']['id'];
		}
		$table = new Cms_Model_DbTable_Template();
		$pairs = $table->fetchPairs('id', 'name', $where);
		$pairs[''] = '';
		ksort($pairs);
		return $pairs;
	}

}
